app.controller("roleAdd",function($scope,$uibModal,$rootScope,del,$state,get,pos,post,$stateParams){
	$scope.search=function(){
		 if($stateParams.id!=''){
		 	 get.list('menu/roleId',{id:$stateParams.id}).then(function(e) {
		  	$scope.list=e.privileges;
		  	$scope.name=e.name
		  	$scope.remarkes=e.remarks
		  	console.log(e)
		  }); 
		 }else{
		 	get.list('menu/privRoleAll').then(function(e) {
		  	$scope.list=e;
		  });
		 }
		
	}
	/**
	 * 提交
	 */
	$scope.sub=function(){
		var arr=''
		angular.forEach($scope.list,function(o){
			if (o.checked==true) {
			     //arr.push(o.pId)	
			     arr+=o.pId+","
			} 
			angular.forEach(o.privileges,function(oo){
				if (oo.checked==true) {
					//arr.push(oo.pId)	
					arr+=oo.pId+","
				}
			})
		})
		var param={
			pIds:arr,
			name:$scope.name,
			remarks:$scope.remarkes
		}
		
		if($stateParams.id!=''){
			console.log(param)
			param.id=$stateParams.id
			post.list("menu/roleUpd",param).then(function(e){
				$rootScope.open(e);
				$state.go("home.role",null,{
					     		reload:true
					     	})
			})
		}else{
			console.log(param)
			post.list("menu/roleAdd",param).then(function(e){
				$rootScope.open(e);
				$state.go("home.role",null,{
					     		reload:true
					     	})
			})
		}
		
		
		
	}
	$scope.search()
	$scope.qx=function(i){
		
    	angular.forEach($scope.list[i].privileges,function(o){
				o.checked=$scope.list[i].checked
			})
    }
	$scope.xz=function(i){
		var arr=[]
	      var fun=$scope.list[i].privileges
	      	angular.forEach(fun,function(o){
				if(o.checked==true){
					arr.push(o)
				}
			})
	    if (arr.length==fun.length) {
	    	$scope.list[i].checked=true
	    } else{
	    	$scope.list[i].checked=false
	    }
	}
})
